package webshop.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Enumeration;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import webshop.data.Person;
import webshop.data.ShoppingBasket;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;

/**
 *
 * @author Simon.
 */
public class PersonLoginServlet extends AbstractSuperServlet {

    @Resource
    UserTransaction tx;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        EntityManager em = emf.createEntityManager();
        PrintWriter out = response.getWriter();


        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache");
        response.setHeader("Expires", "0");
        try {
            tx.begin();
            out.println("<html><head><title>" + this.getClass().getName() + "</title></head><body>");

            HttpSession session = request.getSession();
            String name = request.getParameter("name");

            PersonUtil personUtil = new PersonUtil(em, request);
            Person personlogin = null;
            try {
                personlogin = personUtil.isActive();
                if (personlogin.getName().equals(name)) {
                    out.println(personlogin.getName() + " Already got session.");
                } else {
                    throw new NullPointerException();
                }
            } catch (NullPointerException e) {
                personlogin = personUtil.findPerson(name);
                if (personlogin != null) {
                    session.invalidate();   // Creating a new login we have to erase the old.
                    session = request.getSession(); // And create a new.
                    session.setAttribute(name, personlogin);
                    personlogin = (Person) session.getAttribute(name);
                    out.println("Name found! and stored in session.");
                    ShoppingBasket b = personUtil.getShoppingBasket(personlogin);
                } /*else {
                throw new IllegalStateException("IlligalSession. Login Not found.");
                }*/
            } catch (Exception e) {
            }
            out.println("<pre> Objects stored in sessions: ");
            for (Enumeration e = session.getAttributeNames(); e.hasMoreElements();) {
                out.println(e.nextElement());
            }
            out.println("</pre>");

            out.println("<table>");
            out.println("<tr><th></th></tr>");

            out.println("</table>");
            if (personlogin != null) {
                out.println(personlogin.getName() + " is now logged in!");
            } else {
                out.println("Login name not found");
            }


            tx.commit();
            // 3 sec redirect
            out.println("<meta http-equiv=Refresh content=1;url=customerPage.html> ");
            out.println("<br/>");
            out.println("Redirect in 1 sec.");
            out.println("</body>");
            out.println("</html>");
            em.close();

        } catch (Exception e) {
        }
        out.close();
    }
}
